/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.purpose.service.impl;

import com.koron.common.domain.DataBean;
import com.koron.common.generator.DefaultIdGenerator;
import com.koron.purpose.domain.JobsHazardLibrary;
import com.koron.purpose.mapper.JobsHazardLibraryMapper;
import com.koron.purpose.service.api.JobsHazardLibraryService;
import com.koron.purpose.utils.BusinessCode;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.swan.bean.MessageBean;

import java.util.List;

@Service
public class JobsHazardLibraryServiceImpl implements JobsHazardLibraryService {

    private final static  Logger logger = LoggerFactory.getLogger(JobsHazardLibraryServiceImpl.class);

    @Override
    public MessageBean<Integer> save(JobsHazardLibrary bean) {
        MessageBean<Integer> msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), Integer.class);
        SessionFactory factory = new SessionFactory();
        try {
            JobsHazardLibraryMapper mapper = factory.getMapper(JobsHazardLibraryMapper.class);
            if (StringUtils.isEmpty(bean.getName())) {
                msg.setCode(BusinessCode.HAZARD_LIBRARY_NAME_EMPTY.getCode());
                msg.setDescription(BusinessCode.HAZARD_LIBRARY_NAME_EMPTY.getDescription());
                return msg;
            }
            if (StringUtils.isEmpty(bean.getId())) {
                //新增
                //危害因素名称不能重复
                Integer isDouble = mapper.isExitDoubleName(bean.getName(), null);
                if (isDouble > 0) {
                    msg.setCode(BusinessCode.HAZARD_LIBRARY_NAME_DOUBLE.getCode());
                    msg.setDescription(BusinessCode.HAZARD_LIBRARY_NAME_DOUBLE.getDescription());
                    return msg;
                }
                bean.setId(DefaultIdGenerator.getInstance().generateLongId());
                bean.setAddTime(bean.getUpdateTime());
                bean.setAddUser(bean.getUpdateUser());
                bean.setAddUserName(bean.getUpdateUserName());
                mapper.insert(bean);
                factory.close();
                return msg;
            } else {
                //修改
                //危害因素名称不能重复
                Integer isDouble = mapper.isExitDoubleName(bean.getName(), bean.getId());
                if (isDouble > 0) {
                    msg.setCode(BusinessCode.HAZARD_LIBRARY_NAME_DOUBLE.getCode());
                    msg.setDescription(BusinessCode.HAZARD_LIBRARY_NAME_DOUBLE.getDescription());
                    return msg;
                }
                mapper.update(bean);
                factory.close();
                return msg;
            }
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            if (null != factory) {
                factory.close(false);
            }
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }finally {
            if (null != factory) {
                factory.close();
            }
        }
    }

    @Override
    public MessageBean<Integer> delete(String id) {
        MessageBean<Integer> msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), Integer.class);
        SessionFactory factory = new SessionFactory();
        try {
            if (StringUtils.isEmpty(id)) {
                msg.setCode(BusinessCode.INSUFFICIENT_PARAM.getCode());
                msg.setDescription(BusinessCode.INSUFFICIENT_PARAM.getDescription());
                return msg;
            }
            JobsHazardLibraryMapper mapper = factory.getMapper(JobsHazardLibraryMapper.class);
            mapper.delete(id);
            factory.close(true);
            return msg;
        } catch (Exception e) {
            if (null != factory)
                factory.close(false);
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }finally {
            if (null != factory) {
                factory.close();
            }
        }
    }

    @Override
    public MessageBean<JobsHazardLibrary> queryOne(String id) {
        MessageBean<JobsHazardLibrary> msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), JobsHazardLibrary.class);
        try (SessionFactory factory = new SessionFactory()) {
            if (StringUtils.isEmpty(id)) {
                msg.setCode(BusinessCode.INSUFFICIENT_PARAM.getCode());
                msg.setDescription(BusinessCode.INSUFFICIENT_PARAM.getDescription());
                return msg;
            }
            JobsHazardLibrary bean = factory.getMapper(JobsHazardLibraryMapper.class).queryOneById(id);
            msg.setData(bean);
            return msg;
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }

    @Override
    public MessageBean<DataBean<JobsHazardLibrary>> queryByContion(String corpCode, String corpName, String name, String description, String measures, Integer start, Integer rows) {
        MessageBean<DataBean<JobsHazardLibrary>> msg = new MessageBean<>();
        try (SessionFactory factory = new SessionFactory()) {
            JobsHazardLibraryMapper mapper = factory.getMapper(JobsHazardLibraryMapper.class);
            List<JobsHazardLibrary> list = mapper.queryByCondition(corpCode, corpName, name, description, measures, start, rows);
            Integer totalNumber = mapper.countByCondition(corpCode, corpName, name, description, measures);
            rows = (rows != null && rows > 0) ? rows : totalNumber;
            Integer totalPage = totalNumber > rows ? (totalNumber % rows == 0 ? totalNumber / rows : totalNumber / rows + 1) : 1;
            DataBean<JobsHazardLibrary> dataBean = new DataBean<>();
            dataBean.setTotalPage(totalPage);
            dataBean.setList(list);
            dataBean.setPageSize(rows);
            dataBean.setTotalNumber(totalNumber);
            msg.setCode(BusinessCode.SUCCESS.getCode());
            msg.setDescription(BusinessCode.SUCCESS.getDescription());
            msg.setData(dataBean);
            return msg;
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }
}
